package cn.ulc.socket.weapp.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.annotation.IgnoreAuth;
import cn.ulc.common.util.Result;
import cn.ulc.socket.weapp.model.param.WxUserParam;
import cn.ulc.socket.weapp.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/wechat/account")
public class AccountController {
    private static final Log log = LogFactory.get();
    @Autowired
    AccountService accountService;

    @IgnoreAuth
    @GetMapping("/login")

    public Result login(@Validated String code,Long companyId) {
        return Result.success(accountService.login(code, companyId),true);
    }

    @PostMapping("/bind")
    public Result weChat(@Validated @RequestBody WxUserParam param, BindingResult result, HttpServletRequest request) {
        if(result.hasErrors()) {
            log.error("登录错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(accountService.bind(param,request.getHeader("token").toString()));
    }

    //分享
    @IgnoreAuth
    @GetMapping("/genQrCode")
    public Result genQrCode(String scene,String page) {
        return Result.success(accountService.generateWxQrCode(scene,page),true);
    }


}
